package com.googlecode.cakedroid.utils;

import com.googlecode.cakedroid.ExceptionCallback;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;


public class AppUtils {
	
	/**
	 * share text to installed application.If there are more than one application can handle this intent,
	 * Android system will show a dialog that display a list of application,then let the user choose which to handle
	 * this intent.
	 * @param context The context to use.Usually your Application or Activity object. 
	 * @param dialogTitle  this is used as a title when the system show a dialog to let the user choose application.
	 * @param subject	the subject of the text
	 * @param content	the content you want to share
	 * @param callback	a callback will be called when an exception occurs.this allows you can handle the exception in
	 * 			your way.
	 */
	public static void shareText(final Context context, final String dialogTitle, final String subject,
			final String content, ExceptionCallback callback) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, subject);
		intent.putExtra(Intent.EXTRA_TEXT, content);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try {
			context.startActivity(Intent.createChooser(intent, dialogTitle));
		} catch (ActivityNotFoundException e) {
			if (null != callback) {
				callback.handleException(e);
			}
		}
	}
	
	/**
	 * send email via the proper installed application.Usually use gmail.
	 * @param context The context to use.Usually your Application or Activity object
	 * @param targetEmails	an array of email addresses you want to deliver the email to 
	 * @param subject	the subject of an email.
	 * @param body the body contont of an email.
	 * @param dialogTitle if there are more than one application handling this intent,a dialog will be shown.And this is
	 * 			used as the title of a showing dialog.
	 * @param callback  a callback will be called when an exception occurs.This allows you can handle the exception in
	 * 			your way.
	 */
	public static void sendEmail(final Context context, final String[] targetEmails,
			final String subject, final String body, final String dialogTitle, ExceptionCallback callback) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("plain/text");
		intent.putExtra(Intent.EXTRA_SUBJECT, subject);
		intent.putExtra(Intent.EXTRA_EMAIL, targetEmails);
		intent.putExtra(Intent.EXTRA_TEXT, body);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try {
			context.startActivity(Intent.createChooser(intent, dialogTitle));
		} catch (ActivityNotFoundException e) {
			if (null != callback) {
				callback.handleException(e);
			}
		}
	}
	
	
	
}
